package com.suixun.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.suixun.dao.HomeModuleConfigMapper;
import com.suixun.entity.HomeModuleConfig;
import com.suixun.entity.HomeModuleConfigExample;
import com.suixun.entity.ext.ExtHomeModuleConfig;
import com.suixun.service.HomeModuleConfigService;
import com.suixun.service.common.CommonService;
import com.suixun.util.BeanUtil;
import com.suixun.util.SuixunAssert;

@Service
public class HomeModuleConfigServiceImpl implements HomeModuleConfigService {

	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	@Resource
	private CommonService commonService;
	@Resource
	private HomeModuleConfigMapper homeModuleConfigMapper;
	
	@Transactional
	public void save(List<HomeModuleConfig> list) {
		
		SuixunAssert.assertNull(list, "参数不能为空！");
		
		for(HomeModuleConfig config : list){
			SuixunAssert.assertNull(config.getSchoolId(), "学校ID不能为空！");
			SuixunAssert.assertNull(config.getPindex(), "排序号不能为空！");
			SuixunAssert.assertNull(config.getModuleId(), "首页模块ID不能为空！");
			homeModuleConfigMapper.insert(config);
		}
	}

	@Override
	public void delById(Integer id) {
		
		SuixunAssert.assertNull(id, "参数不能为空！");
		homeModuleConfigMapper.deleteByPrimaryKey(id);
	}

	@Override
	public void delBySchoolid(Integer schoolId) {
		
		SuixunAssert.assertNull(schoolId, "学校ID不能为空！");
		HomeModuleConfigExample example = new HomeModuleConfigExample();
		example.createCriteria().andSchoolIdEqualTo(schoolId);
		
		homeModuleConfigMapper.deleteByExample(example);
	}

	@Transactional
	public void update(List<HomeModuleConfig> list) {
		
		SuixunAssert.assertNull(list, "参数不能为空！");
		
		for(HomeModuleConfig config : list){
			SuixunAssert.assertNull(config.getSchoolId(), "学校ID不能为空！");
			SuixunAssert.assertNull(config.getPindex(), "排序号不能为空！");
			SuixunAssert.assertNull(config.getModuleId(), "首页模块ID不能为空！");
			homeModuleConfigMapper.updateByPrimaryKey(config);
		}
	}

	@Override
	public List<ExtHomeModuleConfig> listHomeModuleConfigBySchoolId(Integer schoolid) {
		
		SuixunAssert.assertNull(schoolid, "参数不能为空！");
		
		Map<String, String> param = new HashMap<String, String>();
		param.put("schoolid", String.valueOf(schoolid));

		List<Map<String, Object>> listMap = commonService.selectList("HomeModuleConfigExtendSql.listBySchoolId", param);
		BeanUtil<ExtHomeModuleConfig> beanUtil = new BeanUtil<ExtHomeModuleConfig>();
		List<ExtHomeModuleConfig> list = null;
		try {
			list = beanUtil.ListMap2JavaBean(listMap, ExtHomeModuleConfig.class);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}

		return list;
	}

}
